﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace IoTEdge.Grpc.Server.AmrEx
{
    public class AmrExLib
    {
        /// <summary>
        /// 获取任务链ID
        /// </summary>
        public static async Task<List<string>> GetTaskChainId(string ip,string port, string name, string token)
        {
            using (HttpClient client = new HttpClient())
            {
                using (HttpRequestMessage reqm = new HttpRequestMessage())
                {
                    reqm.Method = HttpMethod.Get;
                    reqm.RequestUri = new Uri($"http://{ip}:{port}/api/amr/onlineAmr");
                    reqm.Headers.Add("name", name);
                    reqm.Headers.Add("token", token);
                    using var redata1 = await client.SendAsync(reqm);
                    var redata2 = await redata1.Content.ReadAsStringAsync();
                    var obj = (JObject)JsonConvert.DeserializeObject(redata2);
                    List<string> taskids = new List<string>();
                    if (obj != null)
                    {
                        var state = obj["state"];
                        if (state != null)
                        {
                            var array = (JArray)obj["data"];
                            if (array != null)
                            {
                                foreach (JObject item in array)
                                {
                                    var id = item["taskChainId"];
                                    if (id != null)
                                    {
                                        taskids.Add(id.ToString());
                                    }
                                }
                            }
                        }
                    }
                    return taskids;
                }
            }
        }
        //http://192.168.1.131:4333/api/task/cancel/374
        public static async Task StopAmrByTaskChainId(string ip, string port, string name, string token, List<string> ids)
        {
            if (ids == null)
                return;
            for (int i = 0; i < ids.Count; i++)
            {
                using (HttpClient client = new HttpClient())
                {
                    using (HttpRequestMessage reqm = new HttpRequestMessage())
                    {
                        reqm.RequestUri = new Uri($"http://{ip}:{port}/api/task/cancel/{ids[i]}");
                        reqm.Headers.Add("name", name);
                        reqm.Headers.Add("token", token);
                        reqm.Method = HttpMethod.Post;
                        try
                        {
                            using var redata1 = await client.SendAsync(reqm);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
                        }
                    }
                }
            }
        }
    }
}